home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / bildschirmschoner / garshneblanker / gsource / blankers / spotlight / blank.c next >
C/C++ Source or Header  |  1996-04-07  |  6KB  |  273 lines

  1. /*
  2.  *    Copyright (c) 1994 Johan Billing <johan.billing@kcc.ct.se>
  3.  *    All rights reserved.
  4.  *
  5.  *    Michael D. Bayne is free to do whatever he wants with this source...
  6.  *
  7.  */
  8.  
  9. #include <exec/memory.h>
  10. #include <graphics/gfxmacros.h>
  11. #include "/includes.h"
  12.  
  13. #define AREA_SIZE 200
  14. #define NUM_SPOTS ( Prefs[4].po_Level )
  15. #define SPEED ( Prefs[6].po_Level )
  16.  
  17. #define abs( x ) ( (x) < 0 ? -(x) : (x) )
  18.  
  19. struct Spot
  20. {
  21.     LONG x, dx;
  22.     LONG y, dy;
  23. };
  24.  
  25. struct AreaInfo areainfo;
  26. struct TmpRas tmpras;
  27. struct RastPort TmpRP;
  28.  
  29. #include "Spotlight_rev.h"
  30. STATIC const UBYTE VersTag[] = VERSTAG;
  31.  
  32. VOID Defaults( PrefObject *Prefs )
  33. {
  34.     Prefs[0].po_Active = 0;
  35.     Prefs[2].po_Level = 45;
  36.     Prefs[4].po_Level = 4;
  37.     Prefs[6].po_Level = 4;
  38. }
  39.  
  40. VOID MyFreeBitMap( struct BitMap *Map )
  41. {
  42.     LONG i;
  43.  
  44.     if( !Map )
  45.         return;
  46.     
  47.     if( GfxBase->lib_Version > 38 )
  48.     {
  49.         FreeBitMap( Map );
  50.         return;
  51.     }
  52.     
  53.     for( i = 0; i < Map->Depth; i++ )
  54.         if( Map->Planes[i] )
  55.             FreeRaster( Map->Planes[i], Map->BytesPerRow * 8, Map->Rows );
  56.  
  57.     FreeVec( Map );
  58. }
  59.  
  60. struct BitMap *MyAllocBitMap( LONG Width, LONG Height, LONG Depth, LONG Flags,
  61.                              struct BitMap *FriendMap )
  62. {
  63.     struct BitMap *NewMap;
  64.     
  65.     if( GfxBase->lib_Version > 38 )
  66.         return AllocBitMap( Width, Height, Depth, Flags, FriendMap );
  67.     
  68.     if( NewMap = AllocVec( sizeof( struct BitMap ), MEMF_CLEAR ))
  69.     {
  70.         LONG i, Failure = FALSE;
  71.  
  72.         NewMap->BytesPerRow = Width/8 + ( Width%8 ? 1 : 0 );
  73.         NewMap->Rows = Height;
  74.         NewMap->Flags = 0L;
  75.         NewMap->Depth = Depth;
  76.         
  77.         for( i = 0; i < Depth; i++ )
  78.         {
  79.             if(!( NewMap->Planes[i] = AllocRaster( Width, Height )))
  80.             {
  81.                 Failure = TRUE;
  82.                 break;
  83.             }
  84.             if( Flags & BMF_CLEAR )
  85.                 BltClear( NewMap->Planes[i],
  86.                          ( NewMap->BytesPerRow << 16 )|( NewMap->Rows ), 3 );
  87.         }
  88.  
  89.         if( Failure )
  90.         {
  91.             MyFreeBitMap( NewMap );
  92.             NewMap = 0L;
  93.         }
  94.     }
  95.     
  96.     return NewMap;
  97. }
  98.  
  99. LONG Blank( PrefObject *Prefs )
  100. {
  101.     LONG xsize, ysize, xrad, yrad;
  102.     LONG i, RetVal = OK, Colors, ToFrontCount = 0;
  103.     ULONG *ColorTable, PctCount, BPG;
  104.     UBYTE *areabuf;
  105.     PLANEPTR rasbuf;
  106.     struct BitMap *DblMap, *CircleMap, *NullMap;
  107.     struct Spot *Spots;
  108.     struct Screen *Scr;
  109.     struct Window *Wnd;
  110.  
  111.     Spots = AllocVec( sizeof( struct Spot ) * NUM_SPOTS, MEMF_CLEAR );
  112.  
  113.     /* One more plane needed */
  114.     if( Spots &&( Scr = cloneTopScreen( TRUE, Prefs[0].po_Active )))
  115.     {
  116.         xrad = Scr->Width / 8;
  117.         yrad = Scr->Height / 6;
  118.         
  119.         xsize = 2 * ( xrad + 1 ) + 1;
  120.         ysize = 2 * ( yrad + 1 ) + 1;
  121.         
  122.         areabuf = AllocVec( AREA_SIZE, MEMF_CLEAR );
  123.         rasbuf = AllocRaster( Scr->Width, Scr->Height );
  124.         DblMap = MyAllocBitMap( Scr->Width, Scr->Height,
  125.                                Scr->RastPort.BitMap->Depth, BMF_CLEAR,
  126.                                Scr->RastPort.BitMap );
  127.         if( GfxBase->lib_Version > 38 )
  128.             CircleMap = MyAllocBitMap( xsize+1, ysize+1,
  129.                                       Scr->RastPort.BitMap->Depth, BMF_CLEAR,
  130.                                       DblMap );
  131.         else
  132.             CircleMap = MyAllocBitMap( Scr->Width, Scr->Height,
  133.                                       Scr->RastPort.BitMap->Depth, BMF_CLEAR,
  134.                                       DblMap );
  135.         NullMap = MyAllocBitMap( xsize+1, ysize+1, Scr->RastPort.BitMap->Depth,
  136.                                 BMF_CLEAR, DblMap );
  137.         
  138.         if( areabuf && rasbuf && CircleMap && DblMap && NullMap )
  139.         {
  140.             Colors = 1L << Scr->RastPort.BitMap->Depth;
  141.             ColorTable = GetColorTable( Scr );
  142.             
  143.             if( ColorTable )
  144.             {
  145.                 /* Copy lowest colours to highest colours */
  146.                 for( i = 0; i < 3*Colors/2; ++i )
  147.                     ColorTable[i+3*Colors/2+1]=ColorTable[i+1];
  148.             }
  149.             else
  150.             {
  151.                 LONG Col;
  152.                 
  153.                 for( i = 0; i < Colors/2; ++i )
  154.                 {
  155.                     Col = GetRGB4( Scr->ViewPort.ColorMap, i );
  156.                     SetRGB4( &Scr->ViewPort, i + Colors/2,
  157.                             ( Col & 0x0F00 ) >> 8, ( Col & 0x00F0 ) >> 4,
  158.                             ( Col & 0x000F ));
  159.                 }
  160.             }
  161.             
  162.             BPG = AvgBitsPerGun( getTopScreenMode());
  163.             PctCount = ( 1L << BPG ) * Prefs[2].po_Level / 100;
  164.             for( i = 0; i < PctCount; i++ )
  165.                 FadeAndLoadTable( Scr, BPG, ColorTable, 1 );
  166.             
  167.             Wnd = BlankMousePointer( Scr );
  168.             
  169.             for( i = 0; i < NUM_SPOTS; i++ )
  170.             {
  171.                 Spots[i].x = RangeRand( Scr->Width - xsize - 10 ) + 5;
  172.                 Spots[i].y = RangeRand( Scr->Height - ysize - 10 ) + 5;
  173.                 Spots[i].dx = RangeRand( SPEED ) + 1;
  174.                 Spots[i].dy = RangeRand( SPEED ) + 1;
  175.                 Spots[i].dx *= ( RangeRand( 100 ) > 50 ) ? 1 : -1;
  176.                 Spots[i].dy *= ( RangeRand( 100 ) > 50 ) ? 1 : -1;
  177.             }
  178.             
  179.             /* Only change highest plane */
  180.             SetAPen( &Scr->RastPort, Colors/2 );
  181.             SetWrMsk( &Scr->RastPort, Colors/2 );
  182.             
  183.             /* Draw circle */
  184.             InitRastPort( &TmpRP );
  185.             SetWrMsk( &TmpRP, Colors/2 );
  186.             SetAPen( &TmpRP, Colors/2 );
  187.             InitArea( &areainfo, areabuf, AREA_SIZE/5 );
  188.             TmpRP.AreaInfo = &areainfo;
  189.             InitTmpRas( &tmpras, rasbuf, RASSIZE( Scr->Width, Scr->Height ));
  190.             TmpRP.TmpRas = &tmpras;
  191.             TmpRP.BitMap = CircleMap;
  192.             AreaEllipse( &TmpRP, xrad+1, yrad+1, xrad, yrad );
  193.             AreaEnd( &TmpRP );
  194.             
  195.             while( RetVal == OK )
  196.             {
  197.                 for( i = 0; i < NUM_SPOTS; i++ )
  198.                 {
  199.                     BltBitMap( NullMap, 0, 0, DblMap, Spots[i].x, Spots[i].y,
  200.                               xsize, ysize, 0x0C0, Colors/2, 0L );
  201.                     Spots[i].x += Spots[i].dx;
  202.                     Spots[i].y += Spots[i].dy;
  203.                 }
  204.                 
  205.                 for( i = 0; i < NUM_SPOTS; i++ )
  206.                 {
  207.                     if( Spots[i].x < 0 )
  208.                     {
  209.                         Spots[i].x = 0;
  210.                         Spots[i].dx = ( RangeRand( SPEED ) + 1 );
  211.                     }
  212.                     else if( Spots[i].x > Scr->Width - xsize - 1 )
  213.                     {
  214.                         Spots[i].x = Scr->Width - xsize - 1;
  215.                         Spots[i].dx = -( RangeRand( SPEED ) + 1 );
  216.                     }
  217.                     if( Spots[i].y < 0 )
  218.                     {
  219.                         Spots[i].y = 0;
  220.                         Spots[i].dy = ( RangeRand( SPEED ) + 1 );
  221.                     }
  222.                     else if( Spots[i].y > Scr->Height - ysize - 1 )
  223.                     {
  224.                         Spots[i].y = Scr->Height - ysize - 1;
  225.                         Spots[i].dy = -( RangeRand( SPEED ) + 1 );
  226.                     }
  227.                     BltBitMap( CircleMap, 0, 0, DblMap, Spots[i].x, Spots[i].y,
  228.                               xsize, ysize, 0x0E0, Colors/2, 0L );
  229.                 }
  230.  
  231.                 BltBitMap( DblMap, 0, 0, Scr->RastPort.BitMap, 0, 0,
  232.                           Scr->Width, Scr->Height, 0x0C0, Colors/2, 0L );
  233.  
  234.                 if(!( ++ToFrontCount % 60 ))
  235.                     ScreenToFront( Scr );
  236.                 
  237.                 RetVal = ContinueBlanking();
  238.                 WaitBlit();
  239.                 WaitTOF();
  240.             }
  241.             
  242.             UnblankMousePointer( Wnd );
  243.         }
  244.         else
  245.             RetVal = FAILED;
  246.         
  247.         if( NullMap )
  248.             MyFreeBitMap( NullMap );
  249.  
  250.         if( DblMap )
  251.             MyFreeBitMap( DblMap );
  252.         
  253.         if( CircleMap )
  254.             MyFreeBitMap( CircleMap );
  255.         
  256.         if( areabuf )
  257.             FreeVec( areabuf );
  258.         
  259.         if( rasbuf )
  260.             FreeRaster( rasbuf, Scr->Width, Scr->Height );
  261.     }
  262.     else
  263.         RetVal = FAILED;
  264.     
  265.     if( Spots )
  266.         FreeVec( Spots );
  267.  
  268.     if( Scr )
  269.         CloseScreen( Scr );
  270.     
  271.     return RetVal;
  272. }
  273.